using System;
using System.Collections.Generic;
using System.Text;

namespace GriffinNav.GPS.NMEA
{
    // Velocity (speed & direction) Made Good
    internal struct GPVTG
    {
        //$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*33
        //      VTG         Track made good and ground speed
        //      054.7,T     True track made good (degrees)
        //      034.4,M     Magnetic track made good
        //      005.5,N     Ground speed, knots
        //      010.2,K     Ground speed, Kilometers per hour
        //      *33         Checksum

        internal static GPVTG Empty = new GPVTG(string.Empty);
        internal const string PREFIX = "$GPVTG,";
        
        private string[] mVtgParams;

        private Velocity mVelocity;

        internal GPVTG(string vtgString)
        {
            mVelocity = new Velocity();
            mVtgParams = null;

            Update(vtgString);
        }

        internal Velocity Velocity
        {
            get
            {
                return mVelocity;
            }
        }

        internal void Update(string vtgString)
        {
            // check for a value
            if (!string.IsNullOrEmpty(vtgString))
            {
                // remove sentence prefix and leading comma
                mVtgParams = vtgString.Remove(0, PREFIX.Length).Split(new char[] { ',' });

                // check for speed and direction values
                if (!string.IsNullOrEmpty(mVtgParams[4]) && !string.IsNullOrEmpty(mVtgParams[0]))
                {
                    // update speed (knots) and direction
                    mVelocity.Update(decimal.Parse(mVtgParams[4]), decimal.Parse(mVtgParams[0]));
                }
            }
        }
    }
}
